package com.jisu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jisu.common.ResultVo;
import com.jisu.dto.CommentDTO;
import com.jisu.mapper.CommentMapper;
import com.jisu.pojo.Comment;
import com.jisu.service.CommentService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper , Comment> implements CommentService  {

    private final CommentMapper commentMapper;

    public CommentServiceImpl(CommentMapper commentMapper) {
        this.commentMapper = commentMapper;
    }

    //添加评论
    @Override
    public ResultVo<Comment> addComment(Comment comment) {
        if(comment == null){
            return ResultVo.fail("添加失败");
        }
        comment.setCreateTime(LocalDateTime.now());
        commentMapper.insert(comment);
        return ResultVo.success("添加成功",comment);
    }

    //删除评论
    @Override
    public ResultVo deleteComment(List<Integer> ids) {
        if(ids == null || ids.isEmpty()){
            return ResultVo.fail("删除失败,请选择需要删除的id");
        }

        int rows = commentMapper.deleteByIds(ids);
        return ResultVo.success("删除成功",rows);
    }

    //修改评论
    @Override
    public ResultVo updateComment(Comment comment) {
        if(comment == null){
            return ResultVo.fail("修改失败");
        }
        comment.setCreateTime(LocalDateTime.now());
        int rows = commentMapper.updateById(comment);

        if (rows==0){
            return ResultVo.fail("修改失败,未找到对应评论");
        }
        Comment upadtedComment = commentMapper.selectById(comment.getCommentId());
        return ResultVo.success("修改成功",upadtedComment);
    }

    //查询评论
    @Override
    public ResultVo<IPage<Comment>> getCommentInPage(CommentDTO commentDTO) {
        Page<Comment> page = new Page<>(commentDTO.getPageNum(), commentDTO.getPageSize());

        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        if(commentDTO != null){
            wrapper.like(commentDTO.getCommentId() != null, Comment::getCommentId, commentDTO.getCommentId());
            wrapper.like(commentDTO.getCommentDetail() != null, Comment::getCommentDetail, commentDTO.getCommentDetail());
            wrapper.like(commentDTO.getUserId() != null, Comment::getUserId, commentDTO.getUserId());
            wrapper.like(commentDTO.getModelId() != null, Comment::getModelId, commentDTO.getModelId());
        }
        IPage<Comment> list = commentMapper.selectPage(page,wrapper);
        return ResultVo.success("查询成功", list);
    }

    @Override
    public List<Comment> getCommentsByModelId(Integer modelId) {
        if (modelId == null) {
            return List.of(); // 返回空列表
        }

        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Comment::getModelId, modelId);
        return commentMapper.selectList(wrapper);
    }
}
